package controleur;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import modele.Article;
import modele.Commentaire;
import modele.Competition;
import modele.Config;
import modele.FacadeArticle;
import modele.FacadeMembre;
import modele.FacadeMenu;
import modele.FacadeSite;
import modele.InfoSession;
import modele.Membre;
 
/**
 * Servlet implementation class for Servlet: Site
 * Servlet de gestion global du site qui redirige vers l'accueil
 * et d'autres pages si on souhaite en rajouter comme par exemple
 * une FAQ, une page de contacts, etc
 */
 public class Site extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
   @EJB FacadeMenu fmenu;
   @EJB FacadeSite facadeSite;
   @EJB FacadeMembre facadeMembre;
   @EJB FacadeArticle fart;
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public Site() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//Session utilisateur
		HttpSession session = request.getSession(true);
		InfoSession infoSession = (InfoSession)session.getAttribute("infoSession");
		
		String page=request.getParameter("page");
		
		String pageInclue = "accueil.jsp";
		HashMap<String,List<Competition>> menu = fmenu.getMenu();
		
		
		if(page == null || page.equals("") || page.equals("accueil")){			
		
			pageInclue = "accueil.jsp";
		}
		
		if (pageInclue.equals("accueil.jsp")){
			
			ArrayList<Article> articlesCom = (ArrayList<Article>) facadeSite.rechercherArticles(0, Config.nbArticlesParPage, "nombreCommentaires",false, null,null, null, null, null, null, null);
			request.setAttribute("listearticlesCom", articlesCom);
			ArrayList<Article> articlesNote = (ArrayList<Article>) facadeSite.rechercherArticles(0, Config.nbArticlesParPage, "noteMoyenne",false, null,null, null, null, null, null, null);
			request.setAttribute("listearticlesNote", articlesNote);
			ArrayList<Article> articlesDate = (ArrayList<Article>) facadeSite.rechercherArticles(0, Config.nbArticlesParPage, "date",false, null,null, null, null, null, null, null);
			request.setAttribute("listearticlesDate", articlesDate);
			ArrayList<Membre> classement = facadeMembre.getClassementMembres();
			request.setAttribute("classementMembre", classement);
			ArrayList<Commentaire> coms = (ArrayList<Commentaire>) fart.getDerniersCommentairesTriesDate(5);
			request.setAttribute("listecomsDate", coms);
			
			ArrayList<String> resumesArticles = new ArrayList<String>();
			// Initialisation des resumes des articles de l'accueil
			for(Article article: articlesDate){
				String resume = fart.getResume(article);				
				resumesArticles.add(resume);
			}
			request.setAttribute("resumesArticles", resumesArticles);
		}
		
		request.setAttribute("idsSports", this.facadeSite.getIdsSports());
		request.setAttribute("formatDate", Config.sdf);
		request.setAttribute("infoSession", infoSession);
		
		request.setAttribute("Menu", menu);
		request.setAttribute("pageInclue", pageInclue);
		request.getRequestDispatcher("index.jsp").forward(request, response);		
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}   	  	    
}